package com.bq.gametrade.ui.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.bq.gametrade.R;
import com.bq.gametrade.utils.NetworkUtils;
import com.bq.gametrade.utils.ToastUtils;

/**
 * Created by Feel on 2017/1/14.
 */

public class ChangeNoteDialog extends DialogFragment {
    private EditText etFriendNote;
    private Button btnOk,btnCancel;

    public interface OnChangeNoteListener{
        void onNote(String text);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.dialog_change_note, null);
        initView(view);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.dimAmount = 1.0f;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        getDialog().show();
        return view;
    }

    private void initView(View view){
        etFriendNote = (EditText) view.findViewById(R.id.et_friendNote);
        btnOk = (Button) view.findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(etFriendNote.getText().toString())){
                    ToastUtils.showToast(getActivity(),"好友备注还没输入");
                    return;
                }
                if(!NetworkUtils.isNetworkAvailable(getActivity())){
                    ToastUtils.showToast(getActivity(),"网络异常");
                    return;
                }
                dismiss();
                OnChangeNoteListener listener = (OnChangeNoteListener)getActivity();
                listener.onNote(etFriendNote.getText().toString());
            }
        });
        btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
